package com.ejd.web.vo.customer;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.ejd.controller.LoginController;
import com.ejd.service.ICustomerService;
import com.ejd.utils.EncryptUtils;
import com.ejd.utils.MessageFactory;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.web.bo.impl.Customer;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.ManageBeanConstants;
import com.ejd.web.constant.SpringBeanConstants;

@ManagedBean(name="customerUpdatePasswordPage")
@RequestScoped
public class CustomerUpdatePasswordPageBean implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224720L;
	private String oldPassword;
	private String newPassword;
	private String verifyNewPassword;
	public List<String> errorMessages = new ArrayList<String>();
	
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getVerifyNewPassword() {
		return verifyNewPassword;
	}
	public void setVerifyNewPassword(String verifyNewPassword) {
		this.verifyNewPassword = verifyNewPassword;
	}
	
	public List<String> getErrorMessages() {
		return errorMessages;
	}
	public void setErrorMessages(List<String> errorMessages) {
		this.errorMessages = errorMessages;
	}
	public CustomerUpdatePasswordPageBean(){
		
	}
	public String updatePassword(){
		this.getErrorMessages().clear();
		if (!(LoginController.loggedIn())) {
			return EjdConstants.GO_TO_LOGIN;
		}
		ICustomerService<Customer,Long> customerService = (ICustomerService<Customer,Long>) SpringFacesUtil.getSpringBean(SpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		customerService.setEntityClass(Customer.class);
		customerService.setPKClass(Long.class);
		LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
		String customerId = loginCustomerPage.getCustomerInfo().getCustomerId();
		Customer customer = customerService.findUniqueBy("customerId", customerId);
		
		if (null == customer) {
			this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"customerId.notexist.error.message", FacesMessage.SEVERITY_ERROR, customerId).getDetail());
			return EjdConstants.GO_TO_LOGIN;
		}
		String realOldPassword = (String) loginCustomerPage.getCustomerInfo().getPassword();
		String oldPassword = (String) this.getOldPassword();
		oldPassword = EncryptUtils.encryptMD5(oldPassword);
		this.setOldPassword(oldPassword);
		if (!(realOldPassword.equals(oldPassword))) {
			this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"old.password.input.wrong.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
		}
		String newpassword = (String) this.getNewPassword();
		newpassword = EncryptUtils.encryptMD5(newpassword);
		this.setNewPassword(newpassword);
		String verifyNewPassword = (String) this.getVerifyNewPassword();
		verifyNewPassword = EncryptUtils.encryptMD5(verifyNewPassword);
		this.setVerifyNewPassword(verifyNewPassword);
		if (!(newpassword.equals(verifyNewPassword))) {
			this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"password.notmatch.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
		}
		if (this.getErrorMessages().size() > 0) {
			return null;
		}
		if (oldPassword.equals(newpassword)) {
			this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"password.nochange.message", FacesMessage.SEVERITY_ERROR).getDetail());
			return null;
		}
		customer.setPassword(this.getNewPassword());
		customerService.saveOrUpdate(customer);
		loginCustomerPage.setCustomerInfo(customer);
		// set blank data
		this.setOldPassword("");
		this.setNewPassword("");
		this.setVerifyNewPassword("");
		this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"password.update.success.message", FacesMessage.SEVERITY_ERROR).getDetail());
		return null;
	}
}
